import { useSelector } from "react-redux";
import { Menu, MenuProps } from "antd";
import { mainRoutes } from "@/router/main-routes";
import React from "react";

type RouteType = any; // 可根据 mainRoutes 类型进一步细化

function buildMenu(routes: RouteType[], userRole: string): MenuProps['items'] {
  return routes
    .filter((route: any) => route.meta?.isShowNav && (!route.meta?.roles || route.meta.roles.includes(userRole)))
    .map((route: any) => ({
      key: route.path,
      icon: route.meta.icon,
      label: route.meta.title,
      children: route.children ? buildMenu(route.children, userRole) : undefined,
    }));
}

interface SidebarMenuProps extends MenuProps {}

const SidebarMenu: React.FC<SidebarMenuProps> = (props) => {
  const userRole = useSelector((state: any) => state.user.profile?.role || "");
  const items = buildMenu(mainRoutes[0].children, userRole);
  return <Menu items={items} {...props} />;
};

export default SidebarMenu; 